package com.qf.cx.simplenms.topic.tag;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.qf.cx.simplenms.reader.model.AntennaModel;
import com.qf.cx.simplenms.reader.tag.Statistic;
import com.qf.cx.simplenms.reader.tag.Tag;

/**
 * tid 相同认为是同一个标签
 * 
 * @author wanghaming
 *
 */
public class TopicTag {
	private AntennaModel antenna;
	private Tag tag;
	private final Set<String> readerGroupNames = new HashSet<>();
	private final Map<AntennaModel, Statistic> statistics = new HashMap<>();

	public long getDiscoveryTime() {
		if (tag == null) {
			throw new RuntimeException("tag is null");
		}
		return tag.getDiscoveryTime();
	}

	public Map<AntennaModel, Statistic> getStatistics() {
		return statistics;
	}

	public void mergeStatistics(Map<AntennaModel, Statistic> statistics) {
		for (Entry<AntennaModel, Statistic> entry : statistics.entrySet()) {
			Statistic statistic = this.statistics.get(entry.getKey());
			if (statistic == null) {
				this.statistics.put(entry.getKey(), entry.getValue());
			} else {
				statistic.mergeStatistic(entry.getValue());
			}
		}
	}

	public AntennaModel getAntenna() {
		return antenna;
	}

	public void setAntenna(AntennaModel antenna) {
		this.antenna = antenna;
	}

	public Tag getTag() {
		return tag;
	}

	public void setTag(Tag tag) {
		this.tag = tag;
	}

	public Set<String> getReaderGroupNames() {
		return readerGroupNames;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((tag == null) ? 0 : tag.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		TopicTag other = (TopicTag) obj;
		if (tag == null) {
			if (other.tag != null) {
				return false;
			}
		} else if (!tag.equals(other.tag)) {
			return false;
		}
		return true;
	}

	public void addReaderGroupName(String readerGroupName) {
		this.readerGroupNames.add(readerGroupName);
	}

	@Override
	public String toString() {
		return "TopicTag [antenna=" + antenna + ", tag=" + tag + ", readerGroupNames=" + readerGroupNames
				+ ", statistics=" + statistics + "]";
	}
	
}
